package com.delivery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.delivery.entity.Address;
import com.delivery.interceptors.JWTInterceptor;
import com.delivery.result.ApiResult;
import com.delivery.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sixiao
 * @since 2023-04-09
 */
@RestController
@RequestMapping("/api/token/address")
@Api(tags = {"AddressController"},description = "地址页面所有接口均带token")
public class AddressController {

    @Autowired
    private IAddressService addressService;


    @PostMapping("/list")
    @ApiOperation(value = "获取用户所有地址",notes = "需传关键字")
    public ApiResult getAddressListByUserId(@RequestBody Map<String,Object> map){
        Integer userId = Integer.valueOf(JWTInterceptor.userId);
        List<Address> list = addressService.list(
                new LambdaQueryWrapper<Address>().eq(Address::getUserId, userId)
                        .and(map.containsKey("keywords"),
                                i -> i.like(Address::getName ,map.get("keywords").toString())
                                      .or()
                                      .like(Address::getPhone,map.get("keywords").toString())
                        )
        );
        return ApiResult.ok(list);
    }

    @GetMapping("/info")
    @ApiOperation(value = "根据地址id获取地址详情",notes = "需要地址id")
    public ApiResult getAddressInfoById(@ApiParam("参数为：addressId") @RequestParam("addressId") Integer addressId){
        Address byId = addressService.getById(addressId);
        return ApiResult.ok(byId);
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加地址")
    public ApiResult save(@RequestBody Address address){
        Integer user = Integer.valueOf(JWTInterceptor.userId);
        address.setUserId(user);
        boolean save = addressService.save(address);
        return ApiResult.ok(save);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改地址")
    public ApiResult update(@RequestBody Address address){
        Integer userId = Integer.valueOf(JWTInterceptor.userId);
        address.setUserId(userId);
        if (address.getChecked() != null ){
            //将用户下面所有地址都改为非选中
            Address addressBean = new Address();
            addressBean.setChecked(0);
            addressService.update(addressBean, new LambdaQueryWrapper<Address>().eq(Address::getUserId,userId));
        }
        boolean b = addressService.updateById(address);
        return ApiResult.ok(b);
    }

    @ApiOperation(value = "删除地址",notes = "需要地址id")
    @GetMapping("/delete")
    public ApiResult delete(@ApiParam("参数为：id") @RequestParam("id") Integer id){
        boolean b = addressService.removeById(id);
        return ApiResult.ok(b);
    }



}
